﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using NENAData.EF;
using System.IO;
using System.Xml.Linq;

namespace NENAData.NENAMember
{
    public class TcMembership
    {
        /// <summary>
        /// http://telligent.com/community/developers/w/wiki/add-user.aspx
        /// </summary>
        /// <param name="NenaMember"></param>
        public void AddUser(EF.Member NenaMember, string Password, string UserName)
        {

            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            var postUrl = urls.GetUsers();

            //todo: review adding joindate; not currently working
            //account rules
            //http://nena.ca/controlpanel/membership/AccountSettings.aspx
            //orginal email filter; overridden ^[!$&*-=^`|~#%'"+/?_{}\\a-zA-Z0-9 ]+@[a-z0-9]+(?:\.[a-z0-9]+)+$
            StringBuilder strbuilder = new StringBuilder();
            strbuilder.Append("<Request><DisplayName>").Append(NenaMember.FirstName)
                .Append(" ").Append(NenaMember.LastName).Append("</DisplayName>");
            strbuilder.Append("<PrivateEmail>").Append(NenaMember.EmailAddress).Append("</PrivateEmail>");
            strbuilder.Append("<JoinDate>").Append(NenaMember.Application_Date).Append("</JoinDate>");
            strbuilder.Append("<Username>").Append(UserName).Append("</Username>");
            strbuilder.Append("<Password>").Append(Password).Append("</Password>");
            strbuilder.Append("<Location>").Append(NenaMember.City).Append(", ").Append(NenaMember.Province).Append("</Location>");
            strbuilder.Append("</Request>");

            //var data = @"<Request><DisplayName>sample-data</DisplayName><PrivateEmail>sample-eaa98@email.com</PrivateEmail>
            //    <Username>sample-db6d7</Username><Password>sample-data</Password></Request>";
            //            var joe = @"<Request><DisplayName>Joe Blow</DisplayName>
            //                <PrivateEmail>joe@mail.com</PrivateEmail><JoinDate>8/25/2005 7:45:08 AM</JoinDate>
            //                <Username>BlowJoe</Username><Password>limeflow</Password><Location>Manitoba</Location></Request>";

            webClient.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(strbuilder.ToString()));
        }

        /// <summary>
        /// http://telligent.com/community/developers/w/wiki/add-user-to-a-role.aspx
        /// </summary>
        /// <example>
        /// //var data = "<Request><RoleId>80</RoleId></Request>";
        /// </example>
        public void AddUserToRole(int TcUserId, TelligentCommunity.Roles TcRole)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            var postUrl = urls.UserRoles(TcUserId);

            StringBuilder strbuilder = new StringBuilder();

            strbuilder.Append("<Request><RoleId>").Append((int)TcRole).Append("</RoleId></Request>");
            webClient.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(strbuilder.ToString()));
        }

        public void RemoveUserFromRole(int TcUserId, TelligentCommunity.Roles TcRole)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());
            webClient.Headers.Add("Rest-Method", "DELETE");

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            var postUrl = urls.RemoveUserFromRole(TcUserId, (int)TcRole);
            webClient.UploadData(postUrl, "POST", Encoding.ASCII.GetBytes(string.Empty));
        }

        public string GetUserRoles(int UserId)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());
            webClient.Headers.Add("Rest-Method", "GET");

            TelligentCommunity.NenaMembershipUrls urls = new TelligentCommunity.NenaMembershipUrls();
            StringBuilder strbuilder = new StringBuilder();

            var postUrl = strbuilder.Append(urls.GetUserRoles(UserId)).ToString();

            return webClient.DownloadString(postUrl);
        }

        public string GetUserGroups(int UserId)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());
            webClient.Headers.Add("Rest-Method", "GET");

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            var postUrl = urls.GetUserGroups(UserId);

            return webClient.DownloadString(postUrl);
        }
        public string GetGroups()
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());
            webClient.Headers.Add("Rest-Method", "GET");

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            var postUrl = urls.GetGroups();

            return webClient.DownloadString(postUrl);
        }

        /// <summary>
        /// Note this is a *GET*
        /// http://telligent.com/community/developers/w/wiki/get-users.aspx
        /// </summary>
        /// <param name="Email"></param>
        public string GetUser(string Email)
        {
            var webClient = new WebClient();
            webClient.Headers.Add("Rest-User-Token", TelligentCommunity.Connections.NenaRestApi());
            webClient.Headers.Add("Rest-Method", "GET");

            TelligentCommunity.NenaRestUrls urls = new TelligentCommunity.NenaRestUrls();
            StringBuilder strbuilder = new StringBuilder();

            strbuilder.Append(urls.GetUsers()).Append("?EmailAddress=").Append(Email).Append("&IncludeHidden=true");

            return webClient.DownloadString(strbuilder.ToString());
        }



        //public int GetUserId(string Email)
        //{
        //    var doc = GetUser(Email);
        //    XDocument xdoc = XDocument.Parse(doc);
        //    //XDocument root = XDocument.Load(xdoc);

        //    //XElement User = xdoc.Element("User");
        //    //int UserId = (int)User.Element("Id"); 

        //    var Response = xdoc.Element("Response");

        //    var Users = Response.Element("Users");
        //    var User = Users.Element("User");
        //    int UserId = (int)User.Element("Id");

        //    return UserId;

        //}

    }
}
